package UCH.JAVA.SERVLET;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import UCH.JAVA.DAO.*;
import java.util.ArrayList;

import UCH.JAVA.BEAN.*;

public class PersonaServlet extends HttpServlet {
/*richard rondinel -> BlinktOsS*/
    PersonaBean objPersonaBean = null;
    UsuarioBean objUsuarioBean = null;
    MysqlPersonaDAO objPersonaDAO = null;
    MysqlPersonaDAO objUsuarioDAO = null;
    ArrayList lista = null;
    String pagina = "";

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String codigocad = request.getParameter("opcion");
        int codigo = Integer.parseInt(codigocad);

        objPersonaDAO = new MysqlPersonaDAO();
        objUsuarioDAO = new MysqlPersonaDAO();

        switch (codigo) {
            case 1: {
                ReportePersonaGeneral(request, response);
                break;
            }
            case 2: {


                EliminarPersonaGeneral(request, response);
                ReportePersonaGeneral(request, response);
                break;

            }

            case 3: {
                ActualizarPersonaGeneral(request, response);
                ReportePersonaGeneral(request, response);
                break;
            }



            case 5: {

                //getServletContext().getRequestDispatcher("/GrabarUsuario.jsp").forward(request, response);
                GrabarIntegrante(request, response);
                ReportePersonaGeneral(request, response);

                break;
            }
            case 6: {


                GrabarIntegrante(request, response);

                break;
            }


        }

        getServletContext().getRequestDispatcher(pagina).forward(request, response);


    }

    public void ActualizarPersonaGeneral(HttpServletRequest request,
            HttpServletResponse reponse)
            throws ServletException, IOException {
        String usu_idcad = request.getParameter("txtUSU_ID");
        int usu_id = Integer.parseInt(usu_idcad);
        String usuario = request.getParameter("txtUSUARIO");
        String password = request.getParameter("txtPASSWORD");
        String paterno = request.getParameter("txtAPEPATERNO");
        String materno = request.getParameter("txtAPEMATERNO");
        String nombre = request.getParameter("txtNOMBRE");
        String areacad = request.getParameter("txtAREA");

        int area = Integer.parseInt(areacad);


        objUsuarioBean = new UsuarioBean();
        objUsuarioBean.setUsu_id(usu_id);
        objUsuarioBean.setUsu_usuario(usuario);
        objUsuarioBean.setUsu_password(password);
        objUsuarioBean.setUsu_appaterno(paterno);
        objUsuarioBean.setUsu_apmaterno(materno);
        objUsuarioBean.setUsu_nombre(nombre);
        objUsuarioBean.setRol_id(area);

        int i = objUsuarioDAO.ActualizarPersona(objUsuarioBean);
        /*if(i==1)
        {
        request.setAttribute("MENSAJE","Registro Actualizado !!");
        }
        else
        {
        request.setAttribute("MENSAJE","Registro  No Actualizado !!");
        }
        pagina="/Modificar.jsp";*/

    }

    public void ReportePersonaGeneral(HttpServletRequest request,
            HttpServletResponse reponse)
            throws ServletException, IOException {
        lista = objPersonaDAO.ReportePersonas();
        pagina = "/ReporteGeneral.jsp";
        request.setAttribute("LISTADO", lista);
    }

    public void EliminarPersonaGeneral(HttpServletRequest request,
            HttpServletResponse reponse)
            throws ServletException, IOException {


        String codcad[] = request.getParameterValues("elegir");
        int cod[] = new int[codcad.length];
        for (int i = 0; i < cod.length; i++) {
            cod[i] = Integer.parseInt(codcad[i]);
        }







        int flag = objPersonaDAO.EliminarRegistro(cod);
        if (flag == 1 && cod.length > 1) {
            request.setAttribute("mensaje", "Se Elimino los Registros");
        }
        if (flag == 1 && cod.length == 1) {
            request.setAttribute("mensaje", "Se Elimino el Registro");
        }
        if (flag != 1) {
            request.setAttribute("mensaje", "No Se Elimino ningun  Registro");
        }
        pagina = "/ReporteGeneral.jsp";
    }

    public void BuscarPersonaGeneralActualizado(HttpServletRequest request,
            HttpServletResponse reponse)
            throws ServletException, IOException {

        objPersonaBean = new PersonaBean();
        String codcad = request.getParameter("txtcodigo");
        int codigo = Integer.parseInt(codcad);
        objPersonaBean.setCodigo(codigo);
        PersonaBean objPerso = objPersonaDAO.buscarPersona(objPersonaBean);
        request.setAttribute("BUSCAR", objPerso);

        pagina = "/Modificar.jsp";


    }

    public void BuscarPersonaGeneral(HttpServletRequest request,
            HttpServletResponse reponse)
            throws ServletException, IOException {

        objPersonaBean = new PersonaBean();

        String codcad[] = request.getParameterValues("elegir");

        int codigo = Integer.parseInt(codcad[0]);

        objPersonaBean.setCodigo(codigo);

        PersonaBean objPerso = objPersonaDAO.buscarPersona(objPersonaBean);
        request.setAttribute("BUSCAR", objPerso);

        pagina = "/Modificar.jsp";
    }

    public void GrabarIntegrante(HttpServletRequest request,
            HttpServletResponse reponse)
            throws ServletException, IOException {

        //String usu_idcad = request.getParameter("txtUSU_ID");
        
        String usuario = request.getParameter("txtUSUARIO");
        String password = request.getParameter("txtPASSWORD");
        String paterno = request.getParameter("txtAPEPATERNO");
        String materno = request.getParameter("txtAPEMATERNO");
        String nombre = request.getParameter("txtNOMBRE");
        String areacad = request.getParameter("txtAREA");
        int area = Integer.parseInt(areacad);
        //int usu_id = Integer.parseInt(usu_idcad);



        UsuarioBean objintegrantesbeans = new UsuarioBean();

        objintegrantesbeans = new UsuarioBean();
        //objintegrantesbeans.setUsu_id(usu_id);
        objintegrantesbeans.setUsu_usuario(usuario);
        objintegrantesbeans.setUsu_password(password);
        objintegrantesbeans.setUsu_appaterno(paterno);
        objintegrantesbeans.setUsu_apmaterno(materno);
        objintegrantesbeans.setUsu_nombre(nombre);
        objintegrantesbeans.setRol_id(area);



        int i;
        //String mensaje = "";
        MysqlPersonaDAO objDao = new MysqlPersonaDAO();
        i = objDao.GrabarPersona(objintegrantesbeans);
        /*if (i == 1) {
            mensaje = "Registro Insertado";
        } else {
            mensaje = "Registro no insertado";
        }*/
        //request.setAttribute("mensaje", mensaje);
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
